package org.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.entity.DictTypeEntity;
import org.example.service.DictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典类型表
 *
 * @author qersoft
 * @date 2024-12-25
 */
@Slf4j
@RestController
@RequestMapping("backend/dictTypeManagement")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;  // 注入接口

    /**
     * 分页查询字典类型列表
     *
     * @param params 查询条件和分页参数
     * @return 分页数据
     */
    @PostMapping("/list")
    public IPage<DictTypeEntity> list(@RequestBody Map<String, Object> params) {
        return dictTypeService.queryPage(params);
    }

    /**
     * 获取字典类型详情
     *
     * @param dictId 字典类型主键 ID
     * @return 字典类型详情
     */
    @GetMapping("/info/{dictId}")
    public DictTypeEntity info(@PathVariable("dictId") Long dictId) {
        return dictTypeService.getById(dictId);
    }

    /**
     * 新增字典类型
     *
     * @param dictTypeEntity 字典类型实体
     * @return 操作结果
     */
    @PostMapping("/add")
    public String add(@RequestBody DictTypeEntity dictTypeEntity) {
        boolean saved = dictTypeService.save(dictTypeEntity);
        return saved ? "新增成功" : "新增失败";
    }

    /**
     * 更新字典类型
     *
     * @param dictTypeEntity 字典类型实体
     * @return 操作结果
     */
    @PutMapping("/update")
    public String update(@RequestBody DictTypeEntity dictTypeEntity) {
        boolean updated = dictTypeService.updateById(dictTypeEntity);
        return updated ? "更新成功" : "更新失败";
    }

    /**
     * 删除字典类型
     *
     * @param dictId 字典类型主键 ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{dictId}")
    public String delete(@PathVariable("dictId") Long dictId) {
        boolean removed = dictTypeService.removeById(dictId);
        return removed ? "删除成功" : "删除失败";
    }

    /**
     * 根据状态查询字典类型
     *
     * @param status 状态值（0正常, 1停用）
     * @return 字典类型列表
     */
    @GetMapping("/status/{status}")
    public List<DictTypeEntity> listByStatus(@PathVariable Integer status) {
        return dictTypeService.listByStatus(status);
    }
}
